package br.pb.sistcomp.dao;

import br.pb.sistcomp.entity.Lance;
import br.pb.sistcomp.entity.Venda;
import br.pb.sistcomp.util.HibernateUtil;
import java.util.List;
import javax.persistence.EntityManager;

/**
 * @author jefferson 
 */

public class VendaDaoImpl implements VendaDao {

  private  EntityManager em = HibernateUtil.getInstance().getFactory().createEntityManager();

    @Override
    public void cadastrarVenda(Venda venda) {
        em.getTransaction().begin();
        em.persist(venda);
        em.getTransaction().commit();
        em.close();
    }

    @Override
    public void removerVenda(int id) {
        em.remove(id);
        em.getTransaction().commit();
        em.close();
    }

    @Override
    public List<Venda> listarVendasAVista() {
        return em.createQuery("SELECT v FROM Venda v WHERE formaDePagamento=Avista").getResultList();
    }

    @Override
    public List<Venda> listarVendasBoleto() {
        return em.createQuery("SELECT v FROM Vendas v WHERE formaDePagamento=Boleto").getResultList();
    }

    @Override
    public List<Venda> listarVendasCartao() {
        return em.createQuery("SELECT v FROM Vendas v WHERE formaDePagamento=CartaoDeCredito").getResultList();
    }

    @Override
    public Lance getLanceMaisAlto() {
        return (Lance)em.createQuery("SELECT l FROM Lances WHERE l.valor = (SELECT max(valor) FROM Lances valor)").getSingleResult();
    }
}